package cn.iocoder.yudao.module.dr.controller.admin.publish;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.dr.controller.admin.publish.vo.PublishPageReqVO;
import cn.iocoder.yudao.module.dr.controller.admin.publish.vo.PublishRespVO;
import cn.iocoder.yudao.module.dr.controller.admin.publish.vo.PublishSaveReqVO;
import cn.iocoder.yudao.module.dr.dal.dataobject.publish.PublishDO;
import cn.iocoder.yudao.module.dr.service.publish.PublishService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 -  出版申请")
@RestController
@RequestMapping("/dr/publish")
@Validated
public class PublishController {

    @Resource
    private PublishService publishService;

    @PostMapping("/create")
    @Operation(summary = "创建 出版申请")
    @PreAuthorize("@ss.hasPermission('dr:publish:create')")
    public CommonResult<Long> createPublish(@Valid @RequestBody PublishSaveReqVO createReqVO) {
        return success(publishService.createPublish(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新 出版申请")
    @PreAuthorize("@ss.hasPermission('dr:publish:update')")
    public CommonResult<Boolean> updatePublish(@Valid @RequestBody PublishSaveReqVO updateReqVO) {
        publishService.updatePublish(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除 出版申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dr:publish:delete')")
    public CommonResult<Boolean> deletePublish(@RequestParam("id") Long id) {
        publishService.deletePublish(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得 出版申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dr:publish:query')")
    public CommonResult<PublishRespVO> getPublish(@RequestParam("id") Long id) {
        PublishDO publish = publishService.getPublish(id);
        return success(BeanUtils.toBean(publish, PublishRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得 出版申请分页")
    @PreAuthorize("@ss.hasPermission('dr:publish:query')")
    public CommonResult<PageResult<PublishRespVO>> getPublishPage(@Valid PublishPageReqVO pageReqVO) {
        PageResult<PublishDO> pageResult = publishService.getPublishPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PublishRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出 出版申请 Excel")
    @PreAuthorize("@ss.hasPermission('dr:publish:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPublishExcel(@Valid PublishPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PublishDO> list = publishService.getPublishPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, " 出版申请.xls", "数据", PublishRespVO.class,
                        BeanUtils.toBean(list, PublishRespVO.class));
    }

}